
/* <<< COPYRIGHT START >>>
 * Copyright 2006-Present OxygenSoftwareLibrary.com
 * Licensed under the GNU Lesser General Public License.
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * @author: Ugorji Dick-Nwoke
 * <<< COPYRIGHT END >>>
 */


package oxygen.wiki.extensions;

import java.io.File;
import java.io.FileWriter;
import java.util.Hashtable;

import net.sf.ehcache.CacheManager;
import oxygen.util.CloseUtils;
import oxygen.util.EhcacheOxygenCacheManager;
import oxygen.wiki.WikiEngine;
import oxygen.wiki.WikiLocal;

public class EhcacheBasedWikiCacheManager extends EhcacheOxygenCacheManager {
  
  public void prepare() throws Exception {
    WikiEngine we = WikiLocal.getWikiEngine();
    File runtimeDir = we.getRuntimeDirectory();
    File f = new File(runtimeDir, "ehcache_data_cache");
    f.mkdirs();
    
    Hashtable model = new Hashtable();
    model.put("DiskPath", f.getAbsolutePath().replace('\\', '/'));
    
    f = new File(runtimeDir, "ehcache.xml");
    FileWriter fw = new FileWriter(f);
    
    WikiLocal.getWikiEngine().getWikiTemplateFilesHandler().write("ehcache.xml", model, fw);
    CloseUtils.close(fw);
    
    cm = new CacheManager(f.getAbsolutePath());
    //do this. See if it helps fix the "CacheManager is not alive" issues.
    cm.shutdown();
    cm = new CacheManager(f.getAbsolutePath());
  }

}
